home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / dist / glob / tilde.c < prev   
Encoding:
C/C++ Source or Header  |  1992-03-19  |  9.0 KB  |  371 lines

  1. /* tilde.c -- Tilde expansion code (~/foo := $HOME/foo). */
  2.  
  3. /* Copyright (C) 1988,1989, 1991 Free Software Foundation, Inc.
  4.  
  5.    This file is part of GNU Readline, a library for reading lines
  6.    of text with interactive input and history editing.
  7.  
  8.    Readline is free software; you can redistribute it and/or modify
  9.    it under the terms of the GNU General Public License as published by
  10.    the Free Software Foundation; either version 2 of the License, or
  11.    (at your option) any later version.
  12.  
  13.    Readline is distributed in the hope that it will be useful,
  14.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    GNU General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. #include "sysdep.h"
  23.  
  24. #ifndef __MSDOS__
  25. #include <pwd.h>
  26. #endif
  27.  
  28. #ifdef __GNUC__
  29. #undef alloca
  30. #define alloca(x) __builtin_alloca(x)
  31. #endif
  32.  
  33. #ifndef savestring
  34. #define savestring(x) (char *)strcpy (xmalloc (1 + strlen (x)), (x))
  35. #endif
  36.  
  37. typedef int Function ();
  38. #if !defined (NULL)
  39. #  define NULL 0x0
  40. #endif
  41.  
  42. #if defined (TEST)
  43. static char *xmalloc (), *xrealloc ();
  44. #else
  45. extern char *xmalloc (), *xrealloc ();
  46. #endif /* TEST */
  47.  
  48. /* The default value of tilde_additional_prefixes.  This is set to
  49.    whitespace preceding a tilde so that simple programs which do not
  50.    perform any word separation get desired behaviour. */
  51. static char *default_prefixes[] =
  52.   { " ~", "\t~", (char *)NULL };
  53.  
  54. /* The default value of tilde_additional_suffixes.  This is set to
  55.    whitespace or newline so that simple programs which do not
  56.    perform any word separation get desired behaviour. */
  57. static char *default_suffixes[] =
  58.   { " ", "\n", (char *)NULL };
  59.  
  60. /* If non-null, this contains the address of a function to call if the
  61.    standard meaning for expanding a tilde fails.  The function is called
  62.    with the text (sans tilde, as in "foo"), and returns a malloc()'ed string
  63.    which is the expansion, or a NULL pointer if there is no expansion. */
  64. Function *tilde_expansion_failure_hook = (Function *)NULL;
  65.  
  66. /* When non-null, this is a NULL terminated array of strings which
  67.    are duplicates for a tilde prefix.  Bash uses this to expand
  68.    `=~' and `:~'. */
  69. char **tilde_additional_prefixes = default_prefixes;
  70.  
  71. /* When non-null, this is a NULL terminated array of strings which match
  72.    the end of a username, instead of just "/".  Bash sets this to
  73.    `:' and `=~'. */
  74. char **tilde_additional_suffixes = default_suffixes;
  75.  
  76. /* Find the start of a tilde expansion in STRING, and return the index of
  77.    the tilde which starts the expansion.  Place the length of the text
  78.    which identified this tilde starter in LEN, excluding the tilde itself. */
  79. static int
  80. tilde_find_prefix (string, len)
  81.      char *string;
  82.      int *len;
  83. {
  84.   register int i, j, string_len;
  85.   register char **prefixes = tilde_additional_prefixes;
  86.  
  87.   string_len = strlen (string);
  88.   *len = 0;
  89.  
  90.   if (!*string || *string == '~')
  91.     return (0);
  92.  
  93.   if (prefixes)
  94.     {
  95.       for (i = 0; i < string_len; i++)
  96.     {
  97.       for (j = 0; prefixes[j]; j++)
  98.         {
  99.           if (strncmp (string + i, prefixes[j], strlen (prefixes[j])) == 0)
  100.         {
  101.           *len = strlen (prefixes[j]) - 1;
  102.           return (i + *len);
  103.         }
  104.         }
  105.     }
  106.     }
  107.   return (string_len);
  108. }
  109.  
  110. /* Find the end of a tilde expansion in STRING, and return the index of
  111.    the character which ends the tilde definition.  */
  112. static int
  113. tilde_find_suffix (string)
  114.      char *string;
  115. {
  116.   register int i, j, string_len;
  117.   register char **suffixes = tilde_additional_suffixes;
  118.  
  119.   string_len = strlen (string);
  120.  
  121.   for (i = 0; i < string_len; i++)
  122.     {
  123.       if (string[i] == '/' || !string[i])
  124.     break;
  125.  
  126.       for (j = 0; suffixes && suffixes[j]; j++)
  127.     {
  128.       if (strncmp (string + i, suffixes[j], strlen (suffixes[j])) == 0)
  129.         return (i);
  130.     }
  131.     }
  132.   return (i);
  133. }
  134.  
  135. /* Return a new string which is the result of tilde expanding FILENAME. */
  136. char *
  137. tilde_expand (filename)
  138.      char *filename;
  139. {
  140.   char *result, *tilde_expand_word ();
  141.   int result_size, result_index;
  142.  
  143.   result_size = result_index = 0;
  144.   result = (char *)NULL;
  145.  
  146.   /* Scan through FILENAME expanding tildes as we come to them. */
  147.   while (1)
  148.     {
  149.       register int start, end;
  150.       char *tilde_word, *expansion;
  151.       int len;
  152.  
  153.       /* Make START point to the tilde which starts the expansion. */
  154.       start = tilde_find_prefix (filename, &len);
  155.  
  156.       /* Copy the skipped text into the result. */
  157.       /* This test is always true the first time, since result_index
  158.      is 0, result_size is 0, and start is >= 0.  So we malloc here.  */
  159.       if ((result_index + start + 1) > result_size) {
  160.     result_size += (start + 20);
  161.     if (result == NULL)
  162.       result = (char *)xmalloc  (        1 + result_size);
  163.     else
  164.       result = (char *)xrealloc (result, 1 + result_size);
  165.       }
  166.  
  167.       strncpy (result + result_index, filename, start);
  168.       result_index += start;
  169.  
  170.       /* Advance FILENAME upto the starting tilde. */
  171.       filename += start;
  172.  
  173.       /* Make END be the index of one after the last character of the
  174.      username. */
  175.       end = tilde_find_suffix (filename);
  176.  
  177.       /* If both START and END are zero, we are all done. */
  178.       if (!start && !end)
  179.     break;
  180.  
  181.       /* Expand the entire tilde word, and copy it into RESULT. */
  182.       tilde_word = (char *)xmalloc (1 + end);
  183.       strncpy (tilde_word, filename, end);
  184.       tilde_word[end] = '\0';
  185.       filename += end;
  186.  
  187.       expansion = tilde_expand_word (tilde_word);
  188.       free (tilde_word);
  189.  
  190.       len = strlen (expansion);
  191.       if ((result_index + len + 1) > result_size)
  192.     result = (char *)xrealloc (result, 1 + (result_size += (len + 20)));
  193.  
  194.       strcpy (result + result_index, expansion);
  195.       result_index += len;
  196.       free (expansion);
  197.     }
  198.  
  199.   result[result_index] = '\0';
  200.  
  201.   return (result);
  202. }
  203.  
  204. /* Do the work of tilde expansion on FILENAME.  FILENAME starts with a
  205.    tilde.  If there is no expansion, call tilde_expansion_failure_hook. */
  206. char *
  207. tilde_expand_word (filename)
  208.      char *filename;
  209. {
  210.   char *dirname = filename ? savestring (filename) : (char *)NULL;
  211.  
  212.   if (dirname && *dirname == '~')
  213.     {
  214.       char *temp_name;
  215.       if (!dirname[1] || dirname[1] == '/')
  216.     {
  217.       /* Prepend $HOME to the rest of the string. */
  218.       char *temp_home = (char *)getenv ("HOME");
  219.  
  220.       temp_name = (char *)alloca (1 + strlen (&dirname[1])
  221.                       + (temp_home? strlen (temp_home) : 0));
  222.       temp_name[0] = '\0';
  223.       if (temp_home)
  224.         strcpy (temp_name, temp_home);
  225.       strcat (temp_name, &dirname[1]);
  226.       free (dirname);
  227.       dirname = savestring (temp_name);
  228.     }
  229.       else
  230.     {
  231. #ifndef __MSDOS__
  232.       struct passwd *getpwnam (), *user_entry;
  233. #endif
  234.       char *username = (char *)alloca (257);
  235.       int i, c;
  236.  
  237.       for (i = 1; c = dirname[i]; i++)
  238.         {
  239.           if (c == '/')
  240.         break;
  241.           else
  242.         username[i - 1] = c;
  243.         }
  244.       username[i - 1] = '\0';
  245.  
  246. #ifndef __MSDOS__
  247.       if (!(user_entry = getpwnam (username)))
  248.         {
  249.           /* If the calling program has a special syntax for
  250.          expanding tildes, and we couldn't find a standard
  251.          expansion, then let them try. */
  252. #endif
  253.           if (tilde_expansion_failure_hook)
  254.         {
  255.           char *expansion;
  256.  
  257.           expansion =
  258.             (char *)(*tilde_expansion_failure_hook) (username);
  259.  
  260.           if (expansion)
  261.             {
  262.               temp_name = (char *)alloca (1 + strlen (expansion)
  263.                           + strlen (&dirname[i]));
  264.               strcpy (temp_name, expansion);
  265.               strcat (temp_name, &dirname[i]);
  266.               free (expansion);
  267.               goto return_name;
  268.             }
  269.         }
  270.           /* We shouldn't report errors. */
  271. #ifndef __MSDOS__
  272.         }
  273.       else
  274.         {
  275.           temp_name = (char *)alloca (1 + strlen (user_entry->pw_dir)
  276.                       + strlen (&dirname[i]));
  277.           strcpy (temp_name, user_entry->pw_dir);
  278.           strcat (temp_name, &dirname[i]);
  279. #endif
  280.         return_name:
  281.           free (dirname);
  282.           dirname = savestring (temp_name);
  283. #ifndef __MSDOS__
  284.         }
  285.         endpwent ();
  286. #endif
  287.     }
  288.     }
  289.   return (dirname);
  290. }
  291.  
  292. #if defined (TEST)
  293. #undef NULL
  294. #include <stdio.h>
  295.  
  296. main (argc, argv)
  297.      int argc;
  298.      char **argv;
  299. {
  300.   char *result, line[512];
  301.   int done = 0;
  302.  
  303.   while (!done)
  304.     {
  305.       printf ("~expand: ");
  306.       fflush (stdout);
  307.  
  308.       if (!gets (line))
  309.     strcpy (line, "done");
  310.  
  311.       if ((strcmp (line, "done") == 0) ||
  312.       (strcmp (line, "quit") == 0) ||
  313.       (strcmp (line, "exit") == 0))
  314.     {
  315.       done = 1;
  316.       break;
  317.     }
  318.  
  319.       result = tilde_expand (line);
  320.       printf ("  --> %s\n", result);
  321.       free (result);
  322.     }
  323.   exit (0);
  324. }
  325.  
  326. static void memory_error_and_abort ();
  327.  
  328. static char *
  329. xmalloc (bytes)
  330.      int bytes;
  331. {
  332.   char *temp = (char *)malloc (bytes);
  333.  
  334.   if (!temp)
  335.     memory_error_and_abort ();
  336.   return (temp);
  337. }
  338.  
  339. static char *
  340. xrealloc (pointer, bytes)
  341.      char *pointer;
  342.      int bytes;
  343. {
  344.   char *temp;
  345.  
  346.   if (!pointer)
  347.     temp = (char *)malloc (bytes);
  348.   else
  349.     temp = (char *)realloc (pointer, bytes);
  350.  
  351.   if (!temp)
  352.     memory_error_and_abort ();
  353.  
  354.   return (temp);
  355. }
  356.  
  357. static void
  358. memory_error_and_abort ()
  359. {
  360.   fprintf (stderr, "readline: Out of virtual memory!\n");
  361.   abort ();
  362. }
  363.  
  364. /*
  365.  * Local variables:
  366.  * compile-command: "gcc -g -DTEST -o tilde tilde.c"
  367.  * end:
  368.  */
  369. #endif /* TEST */
  370.  
  371.